<template>
  <div>
      <div style="height:35px; ">
          <div style="float:left">
              <el-button style="padding:0 5px" @click="GetSS">添加周六日</el-button>
              <el-date-picker :able="false" type="date" v-model="ldate" placeholder="选择日期" value-format="yyyy/MM/dd" style="width:128px">
              </el-date-picker>
              -
              <el-date-picker :able="false" type="date" v-model="rdate" placeholder="选择日期" value-format="yyyy/MM/dd" style="width:128px">
              </el-date-picker>
          </div>
          <div class="top-deal">
              <el-button @click="btnHolQuery" icon="el-icon-search" type="primary">查询</el-button>
              <el-button @click="btnHolAdd" icon="el-icon-plus">新增</el-button>
              <el-button @click="btnHolDel" icon="el-icon-delete">删除</el-button>
              <el-button @click="btnHolSave" icon="el-icon-document">保存</el-button>
          </div>
      </div>
      <div style="height:430px; ">
          <vxe-table show-overflow ref="hXTable" height="auto" class="holidaystyle" :loading="bLoading" :sort-config="{trigger: 'cell'}" :data.sync="holidayData" border size="mini" show-header-overflow highlight-current-row highlight-hover-row resizable stripe :edit-config="{
      trigger: 'click',
      mode: 'cell',
      showStatus: true,
      showIcon: false,
      }" header-align="center">
              <vxe-table-column field="startdate" title="起始时间" width="130" :formatter="['toDateString', 'yyyy/MM/dd']" sortable>
                  <template v-slot="{row}">
                      <el-date-picker v-model="row.startdate" type="date" placeholder="选择日期" style="width:128px">
                      </el-date-picker>
                  </template>
              </vxe-table-column>
              <vxe-table-column field="enddate" title="结束时间" width="130" sortable>
                  <template v-slot="{row}">
                      <el-date-picker v-model="row.enddate" :able="false" type="date" placeholder="选择日期" value-format="yyyy-MM-dd" style="width:128px">
                      </el-date-picker>
                  </template>
              </vxe-table-column>
              <vxe-table-column field="descriptions" title="备注" width="400" sortable :edit-render="{name: 'input', autoselect: true }">
              </vxe-table-column>
          </vxe-table>
      </div>
  </div>
</template>

<script>
export default {
  name: "rm-holiday",
  props: {
      holidaychecktype: {
          type: String,
          default: "",
          require: true
      },
      //后台打日志用，知道你设置的节假日是干嘛用的
      holidaytitle: {
          type: String,
          default: "节假日",
          require: true
      },
  },
  data() {
      return {
          ldate: new Date(),
          rdate: new Date(),
          bLoading: false,
          holidayData: [],
      };
  },
  created() {
      this.btnHolQuery();
  },
  methods: {
      btnHolQuery() {
          this.$tool
              .request({
                  url: "ucent/holiday",
                  method: "post",
                  data: {
                      action: "holidayQuery",
                      checktype: this.holidaychecktype,
                  },
              })
              .then((res) => {
                  this.holidayData = res.Data;
                  if (res.Data.length > 0) this.$refs.hXTable.setCurrentRow(res.Data[0]);
              })
              .catch((e) => {
                  console.log("error", e);
              });
      },
      btnHolAdd() {
          this.$refs.hXTable.insertAt(
              {
                  checktype: this.holidaychecktype,
                  startdate: "",
                  enddate: "",
                  descriptions: null,
              },
              -1
          );
          this.FunscrollY(this.$refs.hXTable);
      },
      btnHolDel() {
          try {
              let delrow = this.$refs.hXTable.getCurrentRow();
              if (delrow) {
                  var alld = this.$refs.hXTable.tableData;
                  var rowindex = alld.indexOf(delrow);
                  this.$refs.hXTable.remove(delrow);
                  let havedata = this.$refs.hXTable.tableData;
                  if (havedata.length > 0) {
                      if (rowindex >= havedata.length) rowindex = havedata.length - 1;
                      let selectrow = havedata[rowindex];
                      this.HHightLightRow(selectrow);
                  } else {
                      this.HHightLightRow(false);
                  }
              }
          } catch (e) {
              console.log("error", e);
          }
      },
      HHightLightRow(row) {
          if (!!row) {
              this.$refs.hXTable.setCurrentRow(row);
          } else {
              this.$refs.hXTable.setCurrentRow(null);
          }
      },
      async btnHolSave() {
          try {
              const res = await this.$tool.request({
                  url: "ucent/holiday",
                  method: "post",
                  data: {
                      action: "holidaySave",
                      labholidaydetail: this.$refs.hXTable.getRecordset(),
                      holidaytitle: this.holidaytitle,
                  },
              });
              if (res.errcode == "1") {
                  this.$message.success("保存成功！");
              } else {
                  this.$message.error("保存失败！" + res.errmsg);
              }
          } catch (e) {
              this.$alert(e);
          }
      },
      GetSS() {
          //获取周六日
          var bFlag = true;
          var firstday = "";
          var startdays = new Array();
          var enddays = new Array();
          var startno = 0;
          var endno = 0;
          var start;
          start = new Date(Date.parse(this.ldate));
          var end = new Date(Date.parse(this.rdate));
          var getM, getD;
          while (start < end) {
              start = new Date(start.getTime() + 1000 * 60 * 60 * 24);
              var weekx = start.getDay();
              getM = start.getMonth() + 1;
              getD = start.getDate();
              if (getD < 10) getD = "0" + getD;
              if (getM < 10) getM = "0" + getM;
              if ((bFlag && weekx == "0") || (bFlag && weekx == "6")) {
                  bFlag = false;
                  firstday = weekx;
              }
              if (weekx == 6) {
                  startdays[startno] = start.getFullYear() + "-" + getM + "-" + getD + " 00:00:00.000";
                  startno++;
              }
              if (weekx == 0) {
                  enddays[endno] = start.getFullYear() + "-" + getM + "-" + getD + " 00:00:00.000";
                  endno++;
              }
          }
          if (startdays.length == enddays.length) {
              if (firstday == "6") {
                  //(6a,7a)周六日数一致 且为周六开始
                  for (let i = 0; i < startdays.length - 1; i++) {
                      this.$refs.hXTable.insertAt(
                          {
                              checktype: this.holidaychecktype,
                              startdate: startdays[i],
                              enddate: enddays[i],
                              descriptions: "",
                          },
                          -1
                      );
                  }
              } else if (firstday == "0") {
                  this.$refs.hXTable.insertAt(
                      //先插入周日
                      {
                          checktype: this.holidaychecktype,
                          startdate: startdays[i],
                          enddate: enddays[i],
                          descriptions: "",
                      },
                      -1
                  );
                  for (let i = 1; i < enddays.length - 1; i++) {
                      //再循环添加对应的周六日
                      this.$refs.hXTable.insertAt(
                          {
                              checktype: this.holidaychecktype,
                              startdate: startdays[i - 1],
                              enddate: enddays[i],
                              descriptions: "",
                          },
                          -1
                      );
                      this.$refs.hXTable.insertAt(
                          //插入最后多出来的周六
                          {
                              checktype: this.holidaychecktype,
                              startdate: startdays[startdays.length - 1],
                              enddate: startdays[startdays.length - 1],
                              descriptions: "",
                          },
                          -1
                      );
                  }
              }
              this.FunscrollY(this.$refs.hXTable);
          } else {
              if (firstday == "6") {
                  //首日是周六，则多一天周六
                  for (let i = 0; i < enddays.length - 1; i++) {
                      this.$refs.hXTable.insertAt(
                          {
                              checktype: this.holidaychecktype,
                              startdate: startdays[i],
                              enddate: enddays[i],
                              descriptions: "",
                          },
                          -1
                      );
                  }
                  this.$refs.hXTable.insertAt(
                      {
                          checktype: this.holidaychecktype,
                          startdate: startdays[startdays.length - 1],
                          enddate: startdays[startdays.length - 1],
                          descriptions: "",
                      },
                      -1
                  );
                  this.FunscrollY(this.$refs.hXTable);
              } else if ((firstday = "0")) {
                  //首日为周日，则第一次节假日为周日
                  this.$refs.hXTable.insertAt(
                      {
                          checktype: this.holidaychecktype,
                          startdate: enddays[0],
                          enddate: enddays[0],
                          descriptions: "",
                      },
                      -1
                  );
                  for (let i = 1; i < startdays.length - 1; i++) {
                      this.$refs.hXTable.insertAt(
                          {
                              checktype: this.holidaychecktype,
                              startdate: startdays[i],
                              enddate: enddays[i + 1],
                              descriptions: "",
                          },
                          -1
                      );
                  }
              }
          }
          this.$refs.hXTable.insertAt(
              {
                  checktype: this.holidaychecktype,
                  startdate: startdays[startno - 1],
                  enddate: enddays[endno - 1],
                  descriptions: "",
              },
              -1
          );
          this.FunscrollY(this.$refs.hXTable);
      },
      FunscrollY(tablename) {
          this.$nextTick(() => {
              var alldataLen = tablename.tableData.length;
              let L = tablename.tableData[alldataLen - 1];
              tablename.setCurrentRow(L);
              tablename.setActiveRow(L, "startdate");
              tablename.scrollToRow(L);
          });
      },
  },
}
</script>

<style scoped>
.top-deal {
  right: 20px;
  position: absolute;
}
.holidaystyle {
  border: 1px solid #ccc;
}
</style>